class Solution {
public:
    int minSwaps(vector<int>& nums) {
        int num = 0;
        for (auto& x : nums) num += x;
        nums.insert(nums.begin(), 0);
        for (int i = 1; i < nums.size(); i++)
        {
            nums[i] += nums[i - 1];
        }
        int ans = INT_MAX;
        for (int i = 0; i <= num; i++)
        {
            int tail = nums.size() - 1;
            int temp = nums[i] - nums[0];
            temp += nums[tail] - nums[tail - (num - i)];
            temp = num - temp;
            ans = min(ans, temp);
        }
        int l = 1, r = num;
        while (r < nums.size())
        {
            int temp = nums[r++] - nums[l++ - 1];
            temp = num - temp;
            ans = min(ans, temp);
        }
        return ans;
    }
};